package com.sjd.dao;

import com.sjd.pojo.Department;
import com.sjd.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
//员工dao
@Repository
public class EmployeeDao {
    //模拟数据库中的数据
    private static Map<Integer, Employee> employees=null;
    //员工所属的部门
    @Autowired
    private DepartmentDao departmentDao;
    static {
        employees=new HashMap<Integer,Employee>();
        employees.put(101,new Employee(101,"aa","a2320@qq.com",1,new Department(11,"后勤部")));
        employees.put(102,new Employee(102,"bb","b2320@qq.com",0,new Department(12,"市场部")));
        employees.put(103,new Employee(103,"cc","c2320@qq.com",0,new Department(13,"教学部")));
        employees.put(104,new Employee(104,"dd","d2320@qq.com",1,new Department(14,"运营部")));
        employees.put(105,new Employee(105,"ee","e2320@qq.com",1,new Department(15,"教研部")));

    }
    //组件自增
    private static Integer initId=106;
    //增加员工
    public void add(Employee employee){
        if (employee.getId()==null){
            employee.setId(initId++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }
    //查询全部员工信息
    public Collection<Employee> getAll(){
        return employees.values();
    }
    //通过id查询员工
    public Employee getEmployeeByid(Integer id){
        return employees.get(id);
    }
    //删除员工
    public void delete(Integer id){
        employees.remove(id);
    }
}
